package com.itboye.ihomebank.qianyue;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.QianYueDetailBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.web.WebActivity;

public class OrderDetailActivity extends BaseOtherActivity implements Observer {

	String contractNo;
	ImageView close_icon;
	TextView add_shap_title_tv;
	Button huiyue, chakan;
	UserPresenter userPresenter;
	private String uid;
	private QianYueDetailBean detailBean;
	LinearLayout dingjin_detail;
	TextView txt_hetongno, txt_finishtime, txt_hetongstatus, txt_chuzurenname,
			txt_chuzurenmobile, txt_chengzurenname, txt_chengzurenmobile;
	private String password;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.huiyue:
			ByAlert.alert("功能开发中");
			break;
		case R.id.chakan:
			intent = new Intent(this, WebActivity.class);
			intent.putExtra("url", NetPublicConstant.HETONG_URL + "?uid=" + uid
					+ "&psw=" + password + "&contract_no=" + contractNo);
			intent.putExtra("title", "查看合同");
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_orderdetail;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		contractNo = getIntent().getStringExtra("contractNo");
		System.out.println(contractNo + "签约详情");
		add_shap_title_tv.setText("签约详情");
		userPresenter = new UserPresenter(this);
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		userPresenter.qianyueDetail(uid, contractNo);
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		ByAlert.alert(entity.getData());
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.qianyuedetail_success) {
				detailBean = (QianYueDetailBean) entity.getData();
				setInfo(detailBean);
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.qianyuedetail_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

	private void setInfo(QianYueDetailBean detailBean) {
		// TODO Auto-generated method stub
		QianYueDetailBean bean = detailBean;
		if (bean != null) {
			txt_hetongno.setText(bean.getContractNo());
			txt_finishtime.setText(bean.getCompleteTime());
			txt_hetongstatus.setText(bean.getContractStatus());
			txt_chuzurenname.setText(bean.getRenterName());
			txt_chuzurenmobile.setText(bean.getRenterMobile());
			txt_chengzurenname.setText(bean.getOwnerName());
			txt_chengzurenmobile.setText(bean.getOwnerMobile());
			if (bean.getPayInfo() != null) {
				for (int i = 0; i < bean.getPayInfo().size(); i++) {
					View view = LayoutInflater.from(this).inflate(
							R.layout.item_hetong, null);
					TextView textView = (TextView) view.findViewById(R.id.txt);
					TextView txt_device = (TextView) view
							.findViewById(R.id.txt_device);
					EditText editText = (EditText) view.findViewById(R.id.edit);
					editText.setEnabled(false);
					textView.setText("第" + i + "期租金"
							+ bean.getPayInfo().get(i).getItemPrice() + "元");
					editText.setText(bean.getPayInfo().get(i)
							.getItemPayStatus() == "0" ? "等待房客支付" : "支付完成");
					txt_device.setVisibility(View.VISIBLE);
					txt_device.setText(bean.getPayInfo().get(i).getItemDate());
					dingjin_detail.addView(view);
				}
			}
		}
	}

}
